package com.tanhua.dubbo.api;

import com.tanhua.model.em.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;


@DubboService
public class CommentApiImpl implements CommentApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Integer save(Comment comment1) {
        Movement movement = mongoTemplate.findById(comment1.getPublishId(), Movement.class);
        if(movement!=null){
            //在这统一添加动态的用户id
            comment1.setPublishUserId(movement.getUserId());
        }
        //保存到数据库
        mongoTemplate.save(comment1);
       //跟新某个字段，然后返回跟新内容
        Query query =Query.query(Criteria.where("id").is(comment1.getPublishId()));
        Update update =new Update();
        if(comment1.getCommentType()==CommentType.LIKE.getType()){
            update.inc("likeCount",1);
        }else if(comment1.getCommentType()==CommentType.COMMENT.getType()){
            update.inc("commentCount",1);
        }else {
            update.inc("loveCount",1);
        }
        //设置跟新参数
        FindAndModifyOptions options=new FindAndModifyOptions();
        options.returnNew(true);
        Movement movement1 = mongoTemplate.findAndModify(query, update, options, Movement.class);
       return movement1.statisCount(comment1.getCommentType());
    }

    @Override
    //查评论表
    public List<Comment> findComments(Integer page, Integer pagesize, String movementId, CommentType comment) {
        //1、构造查询条件
        if(movementId!=null) {
            Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType")
                    .is(comment.getType());
            Query query = Query.query(criteria)
                    .skip((page-1)*pagesize)
                    .limit(pagesize)
                    .with(Sort.by(Sort.Order.desc("created")));
            return mongoTemplate.find(query,Comment.class);
        }else {
            Criteria criteria = Criteria.where("commentType")
                    .is(comment.getType());
            Query query = Query.query(criteria)
                    .skip((page-1)*pagesize)
                    .limit(pagesize)
                    .with(Sort.by(Sort.Order.desc("created")));
            return mongoTemplate.find(query,Comment.class);
        }
    }

    @Override
    public boolean haslike(String movementId, Long userId, CommentType like) {
        Criteria criteria =Criteria.where("publishId").is(new ObjectId(movementId))
                .and("userId").is(userId).and("commentType").is(like.getType());
        Query query =Query.query(criteria);
        return mongoTemplate.exists(query,Comment.class);
    }

    @Override//删除评论表数据
    public Integer delete(Comment comment) {
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());
        Query query1= Query.query(criteria);
        mongoTemplate.remove(query1,Comment.class);
        //2、修改动态表中的总数量
        //跟新某个字段，然后返回跟新内容
        Query query =Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update =new Update();
        if(comment.getCommentType()==CommentType.LIKE.getType()){
            update.inc("likeCount",-1);
        }else if(comment.getCommentType()==CommentType.COMMENT.getType()){
            update.inc("commentCount",-1);
        }else {
            update.inc("loveCount",-1);
        }
        //设置跟新参数
        FindAndModifyOptions options=new FindAndModifyOptions();
        options.returnNew(true);
        Movement movement1 = mongoTemplate.findAndModify(query, update, options, Movement.class);
        return movement1.statisCount(comment.getCommentType());
    }

    @Override
    public Integer saveCommentLikeCount(String userId) {
        Comment comment = mongoTemplate.findById(userId, Comment.class);
        Query query =Query.query(Criteria.where("id").is(comment.getId()));
        Update update =new Update();
        update.inc("likeCount",1);
        //设置跟新参数
        FindAndModifyOptions options=new FindAndModifyOptions();
        options.returnNew(true);
        Comment comment1 = mongoTemplate.findAndModify(query, update, options, Comment.class);
        return comment1.getLikeCount();
    }

    @Override
    public Integer deleteCommentLikeCount(String userId) {
        Comment comment = mongoTemplate.findById(userId, Comment.class);
        Query query =Query.query(Criteria.where("id").is(comment.getId()));
        Update update =new Update();
        update.inc("likeCount",-1);
        //设置跟新参数
        FindAndModifyOptions options=new FindAndModifyOptions();
        options.returnNew(true);
        Comment comment1 = mongoTemplate.findAndModify(query, update, options, Comment.class);
        return comment1.getLikeCount();
    }

    @Override
    public List<Comment>    findByLoveMeComments(Integer page, Integer pagesize, Long userId, CommentType love) {
            Criteria criteria = Criteria.where("publishUserId").is(userId).and("commentType")
                .is(love.getType());
        Query query = Query.query(criteria)
                .skip((page-1)*pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query,Comment.class);
    }
}
